package restinterpreter;
//http://arun.pv-india.net/AnjarApi/energy_graph

import java.util.Date;

import org.jfree.chart.*;

import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Chart_Test {
	
	private JFreeChart diagramm;
	public ChartPanel cp;
	private double[] plot_data;
	private Date[]	plot_date;
	private int m_data_length;
	
	
	//Constructor
	public Chart_Test()
	{
		init();
	}//end of Chart_Test()
	
	//init-method
	private void init()
	{
		diagramm = ChartFactory.createXYLineChart("Energy Meter Chart", "Time", "kW", null, PlotOrientation.VERTICAL, false, false, false);
		
		DateAxis daxes = new DateAxis();
		
		Date lower_date = new Date();
		lower_date.setHours(0);
		lower_date.setMinutes(0);
		
		Date upper_date = new Date();
		upper_date.setHours(21);
		upper_date.setMinutes(59);
		
		daxes.setRange(lower_date, upper_date);
		diagramm.getXYPlot().setDomainAxis(daxes);
		
		cp = new ChartPanel(diagramm);		
	}
		
	
	
	//get_ChartPanel
	public ChartPanel get_ChartPanel()
	{
		return this.cp;	
	}
	
	//fill plot-data array with values
	 void set_plot_data(int timestamp, double value, int index)
	{
		 if(index==0)
		 {
			 plot_data = new double[m_data_length];
			 plot_date = new Date[m_data_length];
		 }
		Date temp_Date = new Date((long)timestamp*1000);
		plot_date[index] = temp_Date;
		plot_data[index] = value;
	}
	 
	void set_data_length(int length)
	{
		m_data_length = length;
	}
	 
	void plot_data()
	{
		createDataset();
		diagramm.getXYPlot().setDataset(createDataset());
	}
		
	XYDataset createDataset() 
	{	 
		TimeSeries series = new TimeSeries("series");
			
		for(int i=0;i<m_data_length;i++)
	    {
	    	series.add(new Minute(plot_date[i]), plot_data[i]);
	    }
			
		TimeSeriesCollection ds= new TimeSeriesCollection(series);
	    return ds;
	}
}
